package com.j.lemon.learn.basic.singleton;

import java.io.Serializable;

/**
 * @Author lijunjun
 * @Date 2019-04-16 10:51
 * @Description 饿汉式单例
 */
class Singleton3 {
    public static final Singleton3 s = new Singleton3();

    private Singleton3(){

    }
}

/**
 * 饿汉式静态代码块单例
 */
class Singleton2{
    public  static final Singleton2 s;

    static {
        s = new Singleton2();
    }
    private Singleton2(){

    }
}

/**
 * 懒汉式单例
 */
public class Singleton implements Serializable {
    private static volatile Singleton singleton;

    public static Singleton getInstance(){
        if(singleton==null){
            synchronized (Singleton.class){
                if(singleton==null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

    private Singleton(){

    }

    private Object readResolve(){
        return singleton;
    }
}